#ifndef MAIN_WINDW_H
#define MAIN_WINDW_H

#include <QMainWindow>
#include <QtSerialPort/QSerialPort> //串口相关文件
#include <QtSerialPort/QSerialPortInfo>
#include <QTimer>
#include <windows.h>
#include <QString>
#include <QAbstractNativeEventFilter>

#include <QTableWidgetItem>

#include "use_operating.h"

#include <windef.h>
#include <winioctl.h>
#include <dbt.h>

#include "server.h"
#include <QTcpSocket>
#include <QTcpServer>
#include "client.h"
#include "filter_show.h"
#include "serialport.h"
#include "log_save.h"

/*************************/
#define NO_INPUT 0
#define OK_INPUT 1
#define ER_INPUT 2
/*************************/
#define NO_TIME_OUT 1
#define TIME_OUT 0
/*************************/
#define PARAM_ERROR 1
#define PARAM_OK 0
/*************************/

typedef enum run_mode
{
    E_RUN_MODE_NULL = 0,
    E_RUN_MODE_CLIENT = 1,
    E_RUN_MODE_SERVER = 2
} e_run_mode_t;

QT_BEGIN_NAMESPACE
namespace Ui
{
    class Main_windw;
}
QT_END_NAMESPACE

class Main_windw : public QMainWindow, public QAbstractNativeEventFilter
{
    Q_OBJECT
    //! 主窗口父类
public:
    Main_windw(QWidget *parent = nullptr);
    ~Main_windw();

public:
    Ui::Main_windw *ui;

public:
    static void key_events_cb(void);

private slots:
    /*****************串口槽函数*************/
    void Port_event_slot(QString port_name, int state);
    void Serial_event_callback(SerialPort::E_EVENTS_T event, QStringList port_list);

    void serial_read_timer_slot(void);
    void serial_back_time_slot(void);

    void on_open_serial_pushButton_clicked(); // 打开串口的按

    /******************菜单栏触发槽函数*********************/
    void use_operating(void);

    void on_pushButton_3_clicked();

    void on_pushButton_connect_server_clicked();

private: // 窗口类
    filter_show *Filtershow = nullptr;

private:
    /**********************串口部分*************************/
    SerialPort *my_serial = nullptr;
    int Serial_open_state = 0;

    int serial_back_state = NO_INPUT;
    int send_time_out = NO_TIME_OUT;

    Log_save *log_uart = nullptr;

    void Init_Port(void);
    void close_port(void);
    void Refresh_com(QString port_name, int state);

    void Init_param(void);
    void data_process(QByteArray data, qint16 len);

    uint8_t check_serial_state(void);
    void show_user_msg(QString msg);
    /****************************************/

    void register_com_state_msg(void);
    bool nativeEventFilter(const QByteArray &eventType, void *message, long *result);
    /**********************下载部分*************************/
    void show_color_msg(QString strMsg);
    void send_to_host_data(uint8_t *h_buf, uint16_t h_lenth);
    uint8_t wait_serial_data_state(uint32_t time);
    void hardware_recv_process(void);
    uint8_t wait_order(void);
    void clear_serial_state(void);
    int show_my_tips(QString tital, QString text, int state);
    /**************************************************************/
    /**************************设备信息设置****************************/
    void swapEndianness(void *data, uint16_t size);

    /**************** Param setting init ******************/
    typedef struct init_param
    {
        e_run_mode_t run_mode; // 运行模式
        QString host_ip;       // 对方主机IP
        int hex_mode;          // 发送端显示hex
        int hex_mode_recv;     // 接收端显示hex
        int Filter_log_enable;
    } s_init_param_t;

    s_init_param_t init_param;

    void set_sys_params(s_init_param_t init_param);
    void Load_sys_params(void);

    /************** Uart to Net *******************/
    void Uart_to_net(QByteArray data, qint16 len);
    void Net_to_uart(QByteArray data);
    void Net_connect_show_set(int state);

    /******************Filter log*********************/

    uint8_t data_parse(uint8_t *data, uint16_t len);
    void filter_show_start(void);

    /**************Net Work****************/
private:
    Server *my_server = nullptr;
    Client *client = nullptr;
    void Close_server(void);
    void Open_server(void);
    void Close_client(void);
    void Open_client(void);

signals:
    void Data_parse_signal(int direction);
    void Port_event_signal(QString port_name, int state);
    //    void Ui_updateText_signal(QByteArray &text);

private slots:
    void server_recv(QByteArray data);
    void server_connect(QTcpSocket *clientSocket);
    void server_disconnect(QTcpSocket *clientSocket);
    void client_recv(QByteArray data);
    void client_connect(QTcpSocket *ServerSocket);
    void client_disconnect(QTcpSocket *ServerSocket);
    void Data_parse_slot(int direction);
    void on_checkBox_mode_client_clicked(bool checked);
    void on_checkBox_mode_server_clicked(bool checked);
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_checkBox_hex_mode_clicked(bool checked);
    void on_checkBox_ascii_mode_clicked(bool checked);
    void on_checkBox_hex_recv_clicked(bool checked);
    void on_checkBox_ascii_recv_clicked(bool checked);
    void on_checkBox_filter_show_clicked(bool checked);
    void on_lineEdit_protocol_head_editingFinished();
    void on_lineEdit_protocol_tail_editingFinished();
    void on_checkBox_filter_log_clicked(bool checked);
    void filter_show_close_cb(void);
    //      void Ui_updateText_cb(QByteArray &text);
};
#endif // MAIN_WINDW_H
